<template>
  <div class="container" style="width:99%;margin-top:-25px;">
	<!--工具栏-->
	<div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
		<el-form :inline="true" :model="filters" :size="size">
			<el-form-item>
				<el-input v-model="filters.label" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item>
				<kt-button :label="$t('action.search')" perms="sys:genTemplate:view" type="primary" @click="findPage(null)"/>
			</el-form-item>
			<el-form-item>
				<kt-button :label="$t('action.add')" perms="sys:genTemplate:add" type="primary" @click="handleAdd" />
			</el-form-item>
		</el-form>
	</div>
	<!--表格内容栏-->
	<kt-table permsEdit="sys:genTemplate:edit" permsDelete="sys:genTemplate:delete"
		:data="pageResult" :columns="columns" 
		@findPage="findPage" @handleEdit="handleEdit" @handleDelete="handleDelete">
	</kt-table>
	<!--新增编辑界面-->
	<el-dialog :title="operation?'新增':'编辑'" width="40%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
		<el-form :model="dataForm" label-width="80px" :rules="dataFormRules" ref="dataForm" :size="size">
			<el-form-item label="" prop="id"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.id" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="模板名称" prop="templateName"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.templateName" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="minio的bucket" prop="templatePath"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.templatePath" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="" prop="userId"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.userId" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="" prop="templateDesc"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.templateDesc" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="文件名称" prop="fileName"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.fileName" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="组ID" prop="groupId"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.groupId" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="本地路径" prop="localPath"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.localPath" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="模板类型" prop="templateType"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.templateType" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="创建人" prop="crtUserId"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.crtUserId" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="原ID" prop="originalId"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.originalId" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="版本 1 正常版本 2 历史版本" prop="version"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.version" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="crtTime"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.crtTime" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="修改人" prop="mdfUserId"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.mdfUserId" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="修改时间" prop="mdfTime"  v-if="dataForm.isPrimaryKey">
				<el-input v-model="dataForm.mdfTime" auto-complete="off"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
			<el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}</el-button>
		</div>
	</el-dialog>
  </div>
</template>

<script>
import KtTable from "@/views/Core/KtTable"
import KtButton from "@/views/Core/KtButton"
import { format } from "@/utils/datetime"
export default {
	components:{
			KtTable,
			KtButton
	},
	data() {
		return {
			size: 'small',
			filters: {
				label: ''
			},
			columns: [
				{prop:"id", label:"", minWidth:100},
				{prop:"templateName", label:"模板名称", minWidth:100},
				{prop:"templatePath", label:"minio的bucket", minWidth:100},
				{prop:"userId", label:"", minWidth:100},
				{prop:"templateDesc", label:"", minWidth:100},
				{prop:"fileName", label:"文件名称", minWidth:100},
				{prop:"groupId", label:"组ID", minWidth:100},
				{prop:"localPath", label:"本地路径", minWidth:100},
				{prop:"templateType", label:"模板类型", minWidth:100},
				{prop:"crtUserId", label:"创建人", minWidth:100},
				{prop:"originalId", label:"原ID", minWidth:100},
				{prop:"version", label:"版本 1 正常版本 2 历史版本", minWidth:100},
				{prop:"crtTime", label:"创建时间", minWidth:100},
				{prop:"mdfUserId", label:"修改人", minWidth:100},
				{prop:"mdfTime", label:"修改时间", minWidth:100},
			],
			pageRequest: { pageNum: 1, pageSize: 8 },
			pageResult: {},

			operation: false, // true:新增, false:编辑
			editDialogVisible: false, // 新增编辑界面是否显示
			editLoading: false,
			dataFormRules: {
				label: [
					{ required: true, message: '请输入名称', trigger: 'blur' }
				]
			},
			// 新增编辑界面数据
			dataForm: {
				id: null,
				templateName: null,
				templatePath: null,
				userId: null,
				templateDesc: null,
				fileName: null,
				groupId: null,
				localPath: null,
				templateType: null,
				crtUserId: null,
				originalId: null,
				version: null,
				crtTime: null,
				mdfUserId: null,
				mdfTime: null,
			}
		}
	},
	methods: {
		// 获取分页数据
		findPage: function (data) {
			if(data !== null) {
				this.pageRequest = data.pageRequest
			}
			this.pageRequest.columnFilters = {label: {name:'label', value:this.filters.label}}
			this.$api.genTemplate.findPage(this.pageRequest).then((res) => {
				this.pageResult = res.data
			}).then(data!=null?data.callback:'')
		},
		// 批量删除
		handleDelete: function (data) {
			this.$api.genTemplate.batchDelete(data.params).then(data!=null?data.callback:'')
		},
		// 显示新增界面
		handleAdd: function () {
			this.editDialogVisible = true
			this.operation = true
			this.dataForm = {
				id: null,
				templateName: null,
				templatePath: null,
				userId: null,
				templateDesc: null,
				fileName: null,
				groupId: null,
				localPath: null,
				templateType: null,
				crtUserId: null,
				originalId: null,
				version: null,
				crtTime: null,
				mdfUserId: null,
				mdfTime: null,
			}
		},
		// 显示编辑界面
		handleEdit: function (params) {
			this.editDialogVisible = true
			this.operation = false
			this.dataForm = Object.assign({}, params.row)
		},
		// 编辑
		submitForm: function () {
			this.$refs.dataForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editLoading = true
						let params = Object.assign({}, this.dataForm)
						this.$api.genTemplate.save(params).then((res) => {
							if(res.code == 200) {
								this.$message({ message: '操作成功', type: 'success' })
							} else {
								this.$message({message: '操作失败, ' + res.msg, type: 'error'})
							}
							this.editLoading = false
							this.$refs['dataForm'].resetFields()
							this.editDialogVisible = false
							this.findPage(null)
						})
					})
				}
			})
		},
		// 时间格式化
      	dateFormat: function (row, column, cellValue, index){
          	return format(row[column.property])
      	}
	},
	mounted() {
	}
}
</script>

<style scoped>

</style>